// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <pollux/benchmarks/ExpressionBenchmarkBuilder.h>
#include <pollux/functions/lib/benchmarks/FunctionBenchmarkBase.h>
#include <pollux/functions/prestosql/registration/registration_functions.h>
#include <pollux/type/type.h>
#include <pollux/vector/base_vector.h>
#include <pollux/vector/complex_vector.h>
#include <pollux/vector/decoded_vector.h>

using namespace kumo::pollux;

int main(int argc, char** argv) {
  melon::Init init(&argc, &argv);
  memory::MemoryManager::initialize({});
  kumo::pollux::functions::prestosql::registerAllScalarFunctions();

  ExpressionBenchmarkBuilder benchmarkBuilder;

  VectorFuzzer::Options options;
  options.vectorSize = 1'000;
  options.nullRatio = 0.01;

  // Compare regexp_replace with fixed and lambda replacement.
  benchmarkBuilder.addBenchmarkSet("lambda_one_group", ROW({"c0"}, {VARCHAR()}))
      .withFuzzerOptions(options)
      .addExpression("fixed", "regexp_replace(c0, '(\\w)', '$1')")
      .addExpression("lambda", "regexp_replace(c0, '(\\w)', x -> x[1])");
  benchmarkBuilder
      .addBenchmarkSet("lambda_two_groups", ROW({"c0"}, {VARCHAR()}))
      .withFuzzerOptions(options)
      .addExpression("fixed", "regexp_replace(c0, '(\\w)(\\w*)', '$1$2')")
      .addExpression(
          "lambda",
          "regexp_replace(c0, '(\\w)(\\w*)', x -> concat(x[1], x[2]))");

  // Compare regexp_replace with fixed-string pattern and replace.
  benchmarkBuilder
      .addBenchmarkSet("fixed_replacement", ROW({"c0"}, {VARCHAR()}))
      .withFuzzerOptions(options)
      .addExpression("replace", "replace(c0, 'a', '_')")
      .addExpression("regexp_replace", "regexp_replace(c0, 'a','_')");

  benchmarkBuilder.registerBenchmarks();
  benchmarkBuilder.testBenchmarks();

  melon::runBenchmarks();
  return 0;
}
